var userModel = require('../models/users');
const login = {
    //渲染登录注册界面
    login: (req, res, next) => {
        res.render('login')
    },
    //用户注册
    userZhuce: (req, res, next) => {
        userModel.create({ nikename: req.body.name, emil: req.body.emil, phone: req.body.phone, password: req.body.password }).then(result => {
            res.json({
                status: 1,
                msg: '注册成功'
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: '注册失败' + err
            })
        })

    },
    //用户登录
    userLogin: (req, res, next) => {
        var password = req.body.password
        var rember = req.body.rember
        userModel.findOne({ emil: req.body.emil }).then(doc => {
            if (doc) {
                var user = doc;
                if (user.password == password) {
                    req.session.loginUser = user;
                    res.json({
                        status: 1,
                        msg: '登录成功'
                    })
                } else {
                    res.json({
                        status: 0,
                        msg: '密码错误'
                    })
                }
            } else {
                res.json({
                    status: 0,
                    msg: '邮箱不存在'
                })
            }
        }).catch(err => {
            res.redirect('/login');
        })
    }
}
module.exports = login